$(function(){
		

	
	function get_cookie_value(unread){   
		 
		   var cookieArray=document.cookie.split("; "); 
		 
		   for (var i=0;i<cookieArray.length;i++){   
		 
		      var arr=cookieArray[i].split("=");         
		 
		      if(arr[0]==unread)
		    	  return  arr[1];   				 
	   }				 
		   return "none";				 
	} 
	
	 
		 var unread_vl=get_cookie_value("unread");
		   
		  if(unread_vl!="none")
		  { 
		   
			  $.ajax({				
		         type: "post",
		         datatype :"json",
		         url:"/stusys/servlet/Getunread",	
		  
		         success : function(data){
		        	//alert(data.unread);
		        		$("#unread").html("消息("+data.unread+")"); 		        		
		         }
		         });			  
		  }
	

	setInterval(function(){ //刷新 消息
		 var unread_vl=get_cookie_value("unread");
	   
		  if(unread_vl!="none")
		  { 
		   
			  $.ajax({				
		         type: "post",
		         datatype :"json",
		         url:"/stusys/servlet/Getunread",	
		  
		         success : function(data){
		        	//alert(data.unread);
		        		$("#unread").html("消息("+data.unread+")"); 		        		
		         }
		         });			  
		  }
	 }, 60000);
	
	
	$("#getverity").click(
			   function (){
				   document.getElementById('verityimg').src="/stusys/createverity.jsp?a="+new Date().getTime();			 	 
			    });			

	$("#lgbutton").click(function(){
		
		$("#login").css("display","block");	
		$("#bg").css("display","block");	
		$("#getverity").trigger("click");	
		$("#userid").trigger("focus");
	});
	
	$("#alogin").click(function(){
		
	        $("#loading").css("display","block");
		        var  userid=$("#userid").attr("value"); 
				var password=$("#password").attr("value"); 
				var verity=$("#verity").attr("value");
			    $.ajax({
			    	 timeout:30000,
			         type: "post",
			         datatype :"json",
			         url:"servlet/Login",
			        data :{ userid :userid,
			         password : password,
			         verity : verity },			               
			         success : function(data){				      
			         var verity=data.verity;     
			         var password=data.password;
			         var userid=data.userid;
			         var unread=data.unread;
		
			         if(data.logined=="yes")
			        	{
			        	 $("#loading").css("display","none");
			        	 $("#closelg").triggerHandler("click");
			        	 alert("sorry !你已经登录了");
			        	}       
			        else if(verity=="yes")
			        	 {			        	
			           if(password=="yes")
			        	 { 
			        	   $("#lgbutton").attr("href","servlet/User_info")
			        	                 .unbind("click");
			        	                 
			        		$("#lgbutton").attr("id","name")
			        		              .html(userid)	;		        		        
			        		             
			        		
			        	$("#regbutton").html("消息("+unread+")");
			        	  
		 	        	 $("#regbutton").attr("href","servlet/User_news");
		 	        	  $("#regbutton").attr("id","unread");
		 	        	 
		 	        	 $("<a href='servlet/Exit' id='exit'><span>退出</span></a>").insertBefore("#help");		 	        	 
		 	        	 $("#tip").remove();
		 	        	 $("#loading").css("display","none");
			        	 $("#closelg").triggerHandler("click");
			        	 }
			        	 else
			        		{
			        		 $("#pserror").css("display","block"); 
			        		 document.getElementById('verityimg').src="createverity.jsp?a="+new Date().getTime();
			        		 $("#loading").css("display","none");
			        		 $("#verity").val("");
			        		}
			        	 }
			         else 
			        	{ 			        	 			         
			        	  $("#vrtyerror").css("display","block"); 
			        	  document.getElementById('verityimg').src="createverity.jsp?a="+new Date().getTime();	
			        	  $("#loading").css("display","none");
			        	  $("#verity").val("");
			        	}
			          }	        
			       }); 		
		    	}); 
	 $("#verity").focus(function(){
		  $("#vrtyerror").css("display","none"); 
	 });	 
	 $("#password").focus(function(){
		  $("#pserror").css("display","none"); 
	 });
	 $("#closelg").click(function(){
		  $("#loading").css("display","none");
		 $("#login").css("display","none");	
		 $("#bg").css("display","none");
	 });
	
});